<?php
namespace ImiDemo\HttpDemo\MainServer\Controller;

use Imi\Db\Db;
use Imi\Util\Random;
use Imi\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;
use ImiDemo\HttpDemo\MainServer\Model\Test;
use ImiDemo\HttpDemo\MainServer\Model\User;
use ImiDemo\HttpDemo\MainServer\Model\UserSns;
use ImiDemo\HttpDemo\MainServer\Model\UserFriend;
use ImiDemo\HttpDemo\MainServer\Model\UserWithFriend;
use Imi\Util\ObjectArrayHelper;

/**
 * Model操作 演示
 * @Controller("/ModelOneToMany/")
 * @View(renderType="json")
 */
class ModelOneToManyController extends HttpController
{
    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelOneToMany/find
     * 
     * @Action
     * @return void
     */
    public function find()
    {
        $user = UserWithFriend::find(1);
        return $user;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelOneToMany/select
     * 
     * @Action
     * @return void
     */
    public function select()
    {
        $list = UserWithFriend::select();

        return $list;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelOneToMany/newInstance
     * 
     * @Action
     * @return void
     */
    public function newInstance()
    {
        $user = UserWithFriend::newInstance();

        return $user;
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelOneToMany/insert1
     * 
     * @Action
     * @return void
     */
    public function insert1()
    {
        $user = UserWithFriend::newInstance();
        $user->username = Random::letterAndNumber(6, 16);
        $user->ex->intro = '这个人很懒，什么也没留下';
        $user->friends->append(
            UserFriend::newInstance(['friend_user_id'=>1]), 
            UserFriend::newInstance(['friend_user_id'=>2])
        );
        $result = $user->insert();

        return [
            'success'    =>    $result->getLastInsertId() > 0,
            'data'        =>    $user,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelOneToMany/update
     * 
     * @Action
     * @return void
     */
    public function update()
    {
        $user = UserWithFriend::find(1);
        $user->ex->intro = '这个人很懒，什么也没留下-' . date('Y-m-d H:i:s');
        // $friendIds = [4];
        // $user->friends->clear();
        // $updateItems = UserFriend::query()->where('user_id', '=', $user->id)->whereIn('friend_user_id', $friendIds)->select()->getArray();
        // $user->friends->append(...$updateItems);
        // $ids = ObjectArrayHelper::column($updateItems, 'friend_user_id');
        // foreach(array_diff($friendIds, $ids) as $id)
        // {
        //     $user->friends->append(UserFriend::newInstance([
        //         'friend_user_id'    =>    $id,
        //     ]));
        // }
        $user->friends->clear();
        $user->friends->append(UserFriend::newInstance([
            'friend_user_id'    =>    1,
        ]));
        $result = $user->update();

        return [
            'success'    =>    $result->isSuccess(),
            'data'        =>    $user,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelOneToMany/delete
     * 
     * @Action
     * @return void
     */
    public function delete()
    {
        $maxId = UserWithFriend::query()->max('id');
        $user = UserWithFriend::find($maxId);
        $result = $user->delete();

        return [
            'success'    =>    $result->isSuccess(),
        ];
    }
}